package com.main.test;

public class DataUtil {
    private static final byte type = -119;
    private static final byte typeResult = -119;
    private static final String headWrite = "A016018200000000010206";
    private static final String  HEAD_READ = "A0110185000C";
    public static String getEcp(byte[] receivedData) {
        //byte[] byteData = CheckByteCRC.checkCRC("A004018901");
       // byte[] receivedData = {-96,19,1,-119,64,48,0,-30,-128,104,-108,0,0,64,5,-19,-15,-12,42,60,120};

        System.out.println("接收到的字节数组长度: " + receivedData.length);
        for (int i = 0; i < receivedData.length; i++) {
            System.out.print(receivedData[i]+",");
        }

        //取多少位epc
        int ecpCount = getECPCount(receivedData[5]);
        byte[] epc=new byte[ecpCount*2];
        if (receivedData[3]==type){
           // byte a=48;
            for (int i = 0; i < ecpCount*2; i++) {
                //从第七位开始取
                System.out.print("原来epc的值："+receivedData[i+7]+",");
                epc[i]=receivedData[i+7];
                System.out.print("epc的值:"+epc[i]);
            }
        }
        String receivedHexDataEPC = byteArrayToHexString(epc);
        System.out.println("收到服务器返回的十六进制数据: " + receivedHexDataEPC);
        return receivedHexDataEPC;
    }
    //取高五位
    public static int getECPCount(byte len){
        // byte len = 48;
        // 右移 3 位提取高五位
        int highFiveBits = len >> 3;

        // 输出结果
        System.out.println("byte 类型变量 a 的二进制高五位对应的十进制值是: " + highFiveBits);
        return highFiveBits;
    }
    /**
     * 将字节数组转换为十六进制字符串
     * @param bytes 字节数组
     * @return 十六进制字符串
     */
    public static String byteArrayToHexString(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            result.append(String.format("%02X", b & 0xFF));
            result.append(" ");
        }
        return result.toString();
    }
    /**
     * 写epc到卡内
     */
    public static byte[] convertTo12Byte(String epcStr) {
        // 将字符串转换为字节数组
        byte[] inputBytes = epcStr.getBytes();
        System.out.println("输入的epc字符串转byte");
        for (byte b : inputBytes){
            System.out.print(b+" ");
        }
        System.out.println();
        // 创建长度为 12 的目标字节数组
        byte[] result = new byte[12];
        // 复制输入字节数组到目标数组
        System.arraycopy(inputBytes, 0, result, 0, Math.min(inputBytes.length, 12));
        return result;
    }

    public static void main(String[] args) {
        byte[] result = convertTo12Byte("1q2w3e4r");
        for (byte b : result){
            System.out.print(b+",");
        }

        System.out.println("得到的十六进制" + byteArrayToHexString(result));
    }

    public static byte[] getWriteCmd(byte[] epcStr) {
        byte[] head = hexStringToByteArray(headWrite);
        byte[] concatenate = concatenate(head, epcStr);
        for (byte b : concatenate){
            System.out.print(b+",");
        }
        System.out.println();
        //进行crc验证
        return concatenate;
    }
    public static byte[] getWselectCmd(byte[] epcStr) {
        byte[] head = hexStringToByteArray(HEAD_READ);
        byte[] concatenate = concatenate(head, epcStr);
        System.out.println("得到拼接后");
        for (byte b : concatenate){
            System.out.print(b+",");
        }
        System.out.println();
        //进行crc验证
        return concatenate;
    }
    public static byte[] hexStringToByteArray(String hexString) {
        // 实现将十六进制字符串转换为字节数组的逻辑
        // 例如：
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i + 1), 16));
        }
        return data;
    }
    public static byte[] concatenate(byte[] first, byte[] second) {
        // 创建一个新的数组，长度为两个数组长度之和
        byte[] result = new byte[first.length + second.length];
        // 将第一个数组复制到结果数组的前面部分
        System.arraycopy(first, 0, result, 0, first.length);
        // 将第二个数组复制到结果数组的后面部分
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }
    public static void main1(String[] args) {
        String hexString = "112233445566112233445566";
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < hexString.length(); i += 2) {
            // 截取两个字符的十六进制字符串
            String hexPair = hexString.substring(i, i + 2);
            // 将十六进制字符串转换为十进制整数
            int decimal = Integer.parseInt(hexPair, 16);
            // 将十进制整数转换为字符
            char c = (char) decimal;
            result.append(c);
        }

        System.out.println("转换结果: " + result.toString());
    }

    public static String bytesToAscii(byte[] bytes) {
        StringBuilder asciiString = new StringBuilder();
        for (byte b : bytes) {
            // 将字节转换为对应的 ASCII 字符
            char asciiChar = (char) (b & 0xFF);
            asciiString.append(asciiChar);
        }
        return asciiString.toString();
    }

}
